/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var mapa =null;
var userLocation;
var object;
var objects;
var markers = [];

$(document).ready(function(){

    
    $('#map_canvas').gmap().bind('init', function(event, map) {
        
        mapa = map;
        //getBrowserGeoLoc();
        mapa.panTo(new google.maps.LatLng(gmaps_pos.lat,gmaps_pos.lng));
        mapa.setZoom(8);
        
        Init(mapa, this.id);
    //========    
    });
   
    $('#map_canvas2').gmap().bind('init', function(event, map) {
        

        map.panTo(new google.maps.LatLng(gmaps_pos.lat,gmaps_pos.lng));
        map.setZoom(8);
        
        Init(map, this.id);
    //========    
    });

});

function Init(map,ids){
    
    var id = 0;
    var klasa = $('#'+ids).attr('class');
 
    if (/mapmain/.test( klasa) != true){
        
        //=========================
        $(map).click( function(event) {
            
            $('#'+ids).gmap('clear', 'markers');
            map.panTo(event.latLng)
            
            $('#'+ids).gmap('addMarker', {
                'position': event.latLng, 
                'draggable': true, 
                'bounds': false
            }, function(map, marker) {
                $('#dialog').html('<form id="dialog'+marker.__gm_id+'" method="get" action="/" style="display:none;"><p><label for="country">Country</label><input id="country'+marker.__gm_id+'" class="txt" name="country" value=""/></p><p><label for="state">State</label><input id="state'+marker.__gm_id+'" class="txt" name="state" value=""/></p><p><label for="address">Address</label><input id="address'+marker.__gm_id+'" class="txt" name="address" value=""/></p><p><label for="comment">Comment</label><textarea id="comment" class="txt" name="comment" cols="40" rows="5"></textarea></p></form>');
                findLocation(marker.getPosition(), marker);
            });/*.dragend( function(event) {
                var self = this;
                findLocation(event.latLng, this);
            }).click( function() {
                openDialog(this);
            });*/
            
            $('#gmaps_lat').val(event.latLng.lat());
            $('#gmaps_lng').val(event.latLng.lng());
        
        });
    //======================
    }
    
    var marker1 = addMarker(gmaps_pos.lat,gmaps_pos.lng,false,null,ids);
    var mark = new Marker(marker1);
        
    var clk = function() {
        
        $('#'+ids).gmap('openInfoWindow', {
            'content': ''+mark.getContent(),
            'maxWidth': 850,
            'minWidth': 850
        }, marker1);

    };
   
    
    marker1.click(clk);
    if(/map_canas2/.test(ids)){
        clk();
    }
}

function findLocation(location, marker, ids) {
    $('#'+ids).gmap('search', {
        'location': location
    }, function(results, status) {
        if ( status === 'OK' ) {
            $.each(results[0].address_components, function(i,v) {
                if ( v.types[0] == "administrative_area_level_1" || v.types[0] == "administrative_area_level_2" ) {
                    $('#state'+marker.__gm_id).val(v.long_name);
                } else if ( v.types[0] == "country") {
                    $('#country'+marker.__gm_id).val(v.long_name);
                }
            });
            marker.setTitle(results[0].formatted_address);
            $('#address'+marker.__gm_id).val(results[0].formatted_address);
            
            $('#ocity').val(results[0].formatted_address);
        //openDialog(marker);
        }
    });
}

function addMarker(lat,lng,dragable,index,ids){
    
    var lat = new google.maps.LatLng(lat,lng);
    
    var marker = $('#'+ids).gmap('addMarker', {
        'position': lat, 
        'draggable': dragable, 
        'bounds': false,
        'index':index
    });
    
    return marker;
}

function openDialog(el) {
    $('#dialog'+el.__gm_id).dialog({
        'modal':true, 
        'title': 'Edit and save point', 
        'buttons': {
            "Remove": function() {
                $(this).dialog( "close" );
                el.setMap(null);
            },
            "Save": function() {
                $(this).dialog( "close" );
            }
        }
    });
}

function getBrowserGeoLoc(){
    
    if (navigator.geolocation) {
        
        navigator.geolocation.getCurrentPosition( function (position) {
            
            userLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            
            $('#map_canvas').gmap('addMarker', {
                'position': userLocation,
                'bounds': false
            });
            mapa.panTo(userLocation);
        });
    };
}

// map class
function Map(){
    
    return {
        
    };
}

// marker class 
function Marker (gmarker){
    
    this._m = gmarker;
    this._o = gmaps_head;
    
    this.getContent = function(){

        
        var content = $("<div><em class=\"h\">"+this._o.t+"</em>"+
            "<br><p>"+
            this._o.d+"</p>"+
            "</div>");
        
        content.children('p').css({
            'font-weight':'normal',
            'font-size':'12px'
        });
        content.children('h2').css({
            'margin':'0px'
        });
        
        return content.html();
    }
    
    return this;
}

